package com.linkage.educloud.ah.view.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.MyFragmentAdapter;
import com.linkage.educloud.ah.bean.ClazzGroup;
import com.linkage.educloud.ah.mvp.contract.IContactContract;
import com.linkage.educloud.ah.mvp.presenter.ContactPresenter;
import com.linkage.educloud.ah.view.BasicFragment;
import com.zzhou.common.library.util.DisplayUtils;
import com.zzhou.common.library.util.LogUtils;

import java.lang.reflect.Field;
import java.util.List;

import butterknife.BindView;

/**
 * <desc>
 * Created by The Moss on 2018/7/25.
 */

public class ContactFragment extends BasicFragment implements IContactContract.IView {


    @BindView(R.id.fragment_contact_tab)
    TabLayout mTab;
    @BindView(R.id.fragment_contact_viewpager)
    ViewPager mViewPager;
    MyFragmentAdapter mAdapter;

    IContactContract.IPresenter iPresenter;

    public static ContactFragment newInstance() {
        ContactFragment fragment = new ContactFragment();
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_contacts;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iPresenter = new ContactPresenter(this);
        iPresenter.loadDatasFromLocal();
        showZloading();
        iPresenter.loadDatasFromServer();
    }

    @Override
    public void onLoadDatasFromServer(boolean isSuccess, List<ClazzGroup> groups) {

    }

    @Override
    public void onLoadDatasFromLocal(boolean isSuccess, List<ClazzGroup> groups) {
        if (groups == null) {
            LogUtils.e("onLoadDatasFromLocal groups is null");
            return;
        }
        LogUtils.d("onLoadDatasFromLocal groups size = " + groups.size());
        if (mAdapter == null) {
            mAdapter = new MyFragmentAdapter(getChildFragmentManager());
            mViewPager.setAdapter(mAdapter);
            mViewPager.setOffscreenPageLimit(1);
            mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
            mTab.setupWithViewPager(mViewPager);
        }
        mAdapter.setDatas(groups);
        reflex(mTab);
        hideZloading();
    }

    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DisplayUtils.dip2px(10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
